﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

namespace 实验九第三题
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            string xmlFilePath = Server.MapPath(@"Customers.xml");//指定要读取的文件的路径

            try
            {
                XmlReader reader = XmlReader.Create(xmlFilePath);//创建一个XmlReader对象

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)//如果当前读取的节点是元素开始节点
                    {
                        switch (reader.Name)
                        {
                            case "Name":
                                txtName.Text = reader.ReadString();//如果元素名是Name，那么读取节点中的文本值
                                break;
                            case "Email":
                                txtEmail.Text = reader.ReadString();
                                break;
                            case "Street":
                                txtStreet.Text = reader.ReadString();
                                break;
                            case "City":
                                txtCity.Text = reader.ReadString();
                                break;
                            case "State":
                                txtState.Text = reader.ReadString();
                                break;
                            case "ZipCode":
                                txtZipCode.Text = reader.ReadString();
                                break;
                            case "OrderNumber":
                                txtOrderNumber.Text = reader.ReadString();
                                break;
                            case "OrderDate":
                                txtOrderDate.Text = reader.ReadString();
                                break;
                            case "ItemCost":
                                txtItemCost.Text = reader.ReadString();
                                break;
                            case "ItemPrice":
                                txtItemPrice.Text = reader.ReadString();
                                break;
                            case "Customer"://如果是Customer节点
                                if (reader.HasAttributes)//如果当前节点有属性
                                {
                                    txtCustomerID.Text = reader.GetAttribute("CustomerID");//得到当前节点的CustomerID属性
                                }
                                break;
                            case "OrderDetails":
                                if (reader.HasAttributes)
                                {
                                    txtOrderNumber.Text = reader.GetAttribute("OrderNumber");
                                    txtOrderDate.Text = reader.GetAttribute("OrderDate");
                                }
                                break;
                            case "OrderItem":
                                if (reader.HasAttributes)
                                {
                                    txtSKUNumber.Text = reader.GetAttribute("SKUNumber");
                                    txtItemNumber.Text = reader.GetAttribute("ItemNumber");
                                }
                                break;
                        }
                    }
                }
                reader.Close();//关闭XmlReader
            }
            catch (Exception ex)//若捕捉到异常
            {
                Response.Write(ex.Message);//输出异常信息
            }
        }
    }
}